/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import React, { useState, useEffect, useRef } from 'react';
import {
  DisplayPriorityLayout,
  BreakpointManager,
} from '@hadss/web_adaptive_layout_ui';
import './CurPlaySong.css';

import coverImg from '../../../assets/playList/cover.png';
import loveIcon from '../../../assets/playList/love_icon.svg';
import playModeIcon from '../../../assets/playList/play_mode.png';
import controlLeftIcon from '../../../assets/playList/control_left.png';
import playIcon from '../../../assets/playList/play_icon.png';
import controlRightIcon from '../../../assets/playList/control_right.png';
import playListIcon from '../../../assets/playList/play_list_icon.png';

const CurPlaySong = ({ song, bottomAvoidArea }) => {
  const [displayPriorityBoxWidth, setDisplayPriorityBoxWidth] = useState(0);
  const [displayPriorityBoxLeft, setDisplayPriorityBoxLeft] = useState(0);
  const [isLargerPage, setIsLargerPage] = useState(false);
  const [iconSpace, setIconSpace] = useState(0);

  const iconContainerRef = useRef(null);
  const displayPriorityLayoutRef = useRef<DisplayPriorityLayout | null>(null);
  const breakpointManagerRef = useRef(new BreakpointManager());

  const icons = [
    {
      priority: 1,
      source: playModeIcon,
      style: { width: '24px', height: '24px' },
    },
    {
      priority: 1,
      source: controlLeftIcon,
      style: {
        width: '28px',
        height: '28px',
        right: `${iconSpace}px`,
      },
    },
    {
      priority: 10,
      source: playIcon,
      style: { width: '24px', height: '24px' },
    },
    {
      priority: 1,
      source: controlRightIcon,
      style: { width: '28px', height: '28px', left: `${iconSpace}px` },
    },
    {
      priority: 10,
      source: playListIcon,
      style: { width: '24px', height: '24px' },
    },
  ];
  const breakpointManager = breakpointManagerRef.current;

  useEffect(() => {
    const unsubscribe = breakpointManager.subscribeToBreakpoint(() => {
      setDisplayPriorityBoxWidth(
        breakpointManager.useBreakpointValue({
          xs: 70,
          sm: 70,
          md: 212,
          lg: 310,
          xl: 310,
        })
      );
      setDisplayPriorityBoxLeft(
        breakpointManager.useBreakpointValue({
          xs: 0,
          sm: 0,
          md: 0,
          lg: -350,
          xl: -600,
        })
      );
      setIsLargerPage(
        breakpointManager.useBreakpointValue({
          md: false,
          lg: true,
        })
      );
      setIconSpace(
        breakpointManager.useBreakpointValue({
          md: 0,
          lg: 20,
        })
      );
    });
    if (iconContainerRef.current) {
      displayPriorityLayoutRef.current = new DisplayPriorityLayout(
        iconContainerRef.current
      );
      displayPriorityLayoutRef.current?.initializeLayout();
    }

    return () => {
      if (unsubscribe) {
        unsubscribe();
      }
      if (displayPriorityLayoutRef.current) {
        displayPriorityLayoutRef.current.destroy();
        displayPriorityLayoutRef.current = null;
      }
    };
  }, []);

  return (
    <div
      className="bottom-container"
      style={{
        bottom: `${bottomAvoidArea ? bottomAvoidArea : 15}px`,
      }}
    >
      <div className="song-img-and-info">
        <img src={coverImg} className="album-cover" alt="Album cover" />
        <div className="song-info">
          <div className="song-title">{song.title}</div>
          <div className="song-artist">{song.artist}</div>
        </div>
      </div>

      <div style={{ flex: 1 }}></div>
      <div className={isLargerPage ? 'all-icon' : ''}>
        <div
          ref={iconContainerRef}
          data-priority-status="true"
          style={{
            flexDirection: 'row',
            justifyContent: 'space-between',
            alignItems: 'center',
            display: 'flex',
            height: '48px',
            width: `${displayPriorityBoxWidth}px`,
            left: `${displayPriorityBoxLeft}px`,
          }}
        >
          {icons.map((icon, index) => (
            <div
              key={index}
              data-display-priority={icon.priority}
              className="icon-container"
            >
              <img src={icon.source} style={icon.style} alt={`Icon ${index}`} />
            </div>
          ))}
        </div>
      </div>

      {isLargerPage && (
        <img
          src={loveIcon}
          style={{ width: '24px', height: '24px' }}
          alt="Love icon"
        />
      )}
    </div>
  );
};

export default CurPlaySong;
